package com.codered;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class ConnectionChangeReceiver extends BroadcastReceiver {

	/**
	 * Variables
	 */
	
	// Constants
	protected static String TAG = "ConnectionChangeReceiver";
	protected static boolean DEBUG = true;

	
	
	@Override
	public void onReceive(Context context, Intent intent) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
		
		if (activeNetInfo != null && activeNetInfo.isConnected()) {
			// Network connection has been re-established
			// Attempt to upload local files
			if (DEBUG) Log.i(TAG, "Network connection re-established");
			Intent serviceIntent = new Intent(context, UploadService.class);
			context.startService(serviceIntent);
		} else {
			if (DEBUG) Log.i(TAG, "Network connection lost");
		}
	}
		
}